package org.gdpu.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.gdpu.Service.DormitoryService;
import org.gdpu.Service.StudentService;
import org.gdpu.model.Dormitory;
import org.gdpu.model.Student;
import org.gdpu.model.User;
import org.gdpu.vo.StudentVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HEWI2 on 3/4/14.
 */
@Component("myDor")
@Scope("prototype")
public class MyDorAction extends MyActionSupport implements ModelDriven<StudentVO> {
    private StudentService studentService;

    private final StudentVO studentVO = new StudentVO();
    private DormitoryService dormitoryService;

    private Logger logger = LogManager.getLogger(MyDorAction.class.getName());

    @Override
    public String execute() throws Exception {
        HttpSession session = ServletActionContext.getRequest().getSession();
        User person = (User) session.getAttribute("person");
        if (person == null) {
            studentVO.setResultMessage("<script>alert('请登录！');location.href='/login.jsp';</script>");
            return ERROR;
        }

        List<Student> students;
        students = studentService.findStudent("dormitory", person.getDormitory());
        studentVO.setStudents(students);
        view = "/page/myDormitory/myDorPersonList.jsp";
        return VIEW;
    }

    public String findDormitory() {
        if (studentVO.getDormitoryId().isEmpty()) {
            studentVO.setResultMessage("<script>alert('请输入宿舍编号！');location.href='/page/expenseManagement/findExpenseById.jsp';</script>");
            return ERROR;
        } else if (studentVO.getDormitoryId().length() != 4) {
            studentVO.setResultMessage("<script>alert('请输入4位宿舍编号！');location.href='/page/expenseManagement/findExpenseById.jsp';</script>");
            return ERROR;
        }
        Dormitory dormitory = dormitoryService.findById(studentVO.getDormitoryId(), studentVO.getCampus());

        if (dormitory == null) {
            studentVO.setResultMessage("<script>alert('宿舍编号不存在！');location.href='/page/dormitoryManagement/findDormitory.jsp';</script>");
            return ERROR;
        }

        List<Student> students;
        students = studentService.findStudent("dormitory", dormitory);

        studentVO.setStudents(students);
        view = "/page/myDormitory/myDorPersonList.jsp";
        return VIEW;
    }

    @Override
    public StudentVO getModel() {
        return studentVO;
    }

    @Resource(name = "studentServiceImpl")
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @Resource(name = "dormitoryServiceImpl")
    public void setDormitoryService(DormitoryService dormitoryService) {
        this.dormitoryService = dormitoryService;
    }
}
